﻿using Newtonsoft.Json;
using Omabar.BLL;
using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace Omabar.CMS.Handler
{
    /// <summary>
    /// RouteDelete 的摘要说明
    /// </summary>
    public class RouteDelete : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            RESP_Info info = new RESP_Info();

            try
            {
                context.Response.ContentType = "text/plain";
                bool batch = string.IsNullOrEmpty(context.Request["batch"]) ? false : bool.Parse(context.Request["batch"]);
                Route_BLL bll = new Route_BLL();
                CityBelong_BLL cbBLL = new CityBelong_BLL();
                Route_Hotel_BLL rhBLL = new Route_Hotel_BLL();
                Route_SpecialItem_BLL rsBLL = new Route_SpecialItem_BLL();
                Route_Characteristic_BLL rcBLL = new Route_Characteristic_BLL();
                Route_Theme_BLL rtBLL = new Route_Theme_BLL();
                Route_Gallery_BLL rgBLL = new Route_Gallery_BLL();
                Route_Trip_BLL rtpBLL = new Route_Trip_BLL();
                Route_Package_BLL rpBLL = new Route_Package_BLL();
                if (batch)
                {
                    string[] ids = context.Request["ids[]"].Split(',');

                    foreach (string id in ids)
                    {
                        info = bll.Delete(new ROUTE() { ROUTE_ID = id });

                        cbBLL.ExecuteSQL(string.Format("DELETE FROM ROUTE_COUNTRY WHERE ROUTE_ID='{0}'", id));
                        rhBLL.ExecuteSQL(string.Format("DELETE FROM ROUTE_HOTEL WHERE ROUTE_ID='{0}'", id));
                        rsBLL.ExecuteSQL(string.Format("DELETE FROM ROUTE_SPECIALITEM WHERE ROUTE_ID='{0}'", id));
                        rcBLL.ExecuteSQL(string.Format("DELETE FROM ROUTE_CHARACTERISTIC WHERE ROUTE_ID='{0}'", id));
                        rtBLL.ExecuteSQL(string.Format("DELETE FROM ROUTE_THEME WHERE ROUTE_ID='{0}'", id));
                        rgBLL.ExecuteSQL(string.Format("DELETE FROM ROUTE_GALLERY WHERE ROUTE_ID='{0}'", id));
                        rtpBLL.ExecuteSQL(string.Format("DELETE FROM ROUTE_TRIP WHERE ROUTE_ID='{0}'", id));
                        rpBLL.ExecuteSQL(string.Format("DELETE FROM ROUTE_PACKAGE WHERE ROUTE_ID='{0}'", id));
                    }
                }
                else
                {
                    string id = context.Request["id"];
                    info = bll.Delete(new ROUTE() { ROUTE_ID = id });

                    cbBLL.ExecuteSQL(string.Format("DELETE FROM ROUTE_COUNTRY WHERE ROUTE_ID='{0}'", id));
                    rhBLL.ExecuteSQL(string.Format("DELETE FROM ROUTE_HOTEL WHERE ROUTE_ID='{0}'", id));
                    rsBLL.ExecuteSQL(string.Format("DELETE FROM ROUTE_SPECIALITEM WHERE ROUTE_ID='{0}'", id));
                    rcBLL.ExecuteSQL(string.Format("DELETE FROM ROUTE_CHARACTERISTIC WHERE ROUTE_ID='{0}'", id));
                    rtBLL.ExecuteSQL(string.Format("DELETE FROM ROUTE_THEME WHERE ROUTE_ID='{0}'", id));
                    rgBLL.ExecuteSQL(string.Format("DELETE FROM ROUTE_GALLERY WHERE ROUTE_ID='{0}'", id));
                    rtpBLL.ExecuteSQL(string.Format("DELETE FROM ROUTE_TRIP WHERE ROUTE_ID='{0}'", id));
                    rpBLL.ExecuteSQL(string.Format("DELETE FROM ROUTE_PACKAGE WHERE ROUTE_ID='{0}'", id));
                }
            }
            catch (Exception ex)
            {
                info.Success = false;
                info.Title = "错误";
                info.Tip = "路线删除失败";
                info.Url = "RouteList.aspx";
            }

            context.Response.Write(JsonConvert.SerializeObject(info));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}